package cn.wei.reallyEnjoy.app;


import android.os.Bundle;
import android.widget.FrameLayout;
import android.widget.RadioGroup;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;
import androidx.fragment.app.FragmentTransaction;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;
import cn.wei.reallyEnjoy.R;
import cn.wei.reallyEnjoy.base.BaseFragment;
import cn.wei.reallyEnjoy.home.fragment.HomeFragment;
import cn.wei.reallyEnjoy.user.fragment.UserFragment;

public class MainActivity extends FragmentActivity {


    @BindView(R.id.frameLayout)
    FrameLayout frameLayout;
    @BindView(R.id.rg_main)
    RadioGroup rgMain;
    //装多个fragment的实例集合
    private ArrayList<BaseFragment> fragments;

    private int position = 0;

    /**
     * 缓存的Fragment，或者上次显示的Fragment
     */
    private Fragment tempFragment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //ButterKnife和当前Activity绑定
        ButterKnife.bind(this);

        //初始化各个fragment，
        initFragment();

        //设置RadioGroup监听
        initListener();



    }

    private void initListener() {


        //设置切换
        rgMain.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId) {
                    case R.id.rb_home://首页
                        position = 0;
                        break;

                    case R.id.rb_user://我的
                        position = 1;
                        break;
                    default:
                        position = 0;
                        break;
                }
                //根据位置取不同的Flagment
                BaseFragment baseFragment = getFragment(position);

                //切换Fragment
                switchFragment(tempFragment, baseFragment);

            }
        });
        //设置默认选中
        rgMain.check(R.id.rb_home);

    }

    /**
     * 初始化各个fragment，
     * 按照顺序添加
     */
    protected void initFragment() {
        fragments = new ArrayList<>();
        fragments.add(new HomeFragment());
        fragments.add(new UserFragment());
    }

    /**
     * 根据位置获取不同的Fragment
     *
     * @param position
     * @return
     */
    private BaseFragment getFragment(int position) {
        if (fragments != null && fragments.size() > 0) {
            BaseFragment baseFragment = fragments.get(position);
            return baseFragment;
        }
        return null;
    }

    /**
     * @param formFragment：上次显示的Fragment
     * @param nextFragment：当前要显示的Fragment
     */
    private void switchFragment(Fragment formFragment, BaseFragment nextFragment) {
        if (tempFragment != nextFragment) {
            tempFragment = nextFragment;
            if (nextFragment != null) {
                FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
                //判断nextFragment是否添加
                if (!nextFragment.isAdded()) {
                    //隐藏当前Fragment
                    if (formFragment != null) {
                        transaction.hide(formFragment);
                    }
                    transaction.add(R.id.frameLayout, nextFragment).commit();
                } else {
                    //隐藏当前Fragment
                    if (formFragment != null) {
                        transaction.hide(formFragment);
                    }
                    transaction.show(nextFragment).commit();
                }
            }
        }
    }
}